<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Goods
 * @package App\Models
 * @version September 14, 2017, 7:41 pm UTC
 *
 * @property string sn
 * @property string name
 * @property integer click_count
 * @property integer sales_count
 * @property string brand
 * @property string provider_name
 * @property integer number
 * @property decimal market_price
 * @property decimal sale_price
 * @property string unit
 * @property timestamp sale_start_date
 * @property timestamp sale_end_date
 * @property integer is_on_sale
 * @property integer is_best
 * @property integer is_new
 * @property integer is_hot
 * @property integer sort
 * @property string desc
 * @property string image
 */
class Goods extends Model
{
    use SoftDeletes;

    public $table = 'goods';
    

    protected $dates = ['deleted_at'];


    public $fillable = [
        'shop_id',
        'sn',
        'name',
        'title',
        'click_count',
        'sales_count',
        'brand',
        'provider_name',
        'number',
        'market_price',
        'sale_price',
        'unit',
        'weight',
        'sale_start_date',
        'sale_end_date',
        'is_on_sale',
        'is_best',
        'is_new',
        'is_hot',
        'sort',
        'desc',
        'image',
        'day_buy_number',
        'is_timely_sort',
        'freight_id',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'sn' => 'string',
        'name' => 'string',
        'click_count' => 'integer',
        'sales_count' => 'integer',
        'brand' => 'string',
        'provider_name' => 'string',
        'number' => 'integer',
        'unit' => 'string',
        'is_on_sale' => 'integer',
        'is_best' => 'integer',
        'is_new' => 'integer',
        'is_hot' => 'integer',
        'sort' => 'integer',
        'desc' => 'string',
        'image' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        
    ];

    public function categories()
    {
        return $this->belongsToMany('App\Models\Category','category_goods','goods_id','cat_id');
    }

    public function attributes()
    {
        return $this->hasMany('App\Models\GoodsAttribute');
    }

    public function attributesByStatus()
    {
        return $this->attributes()->where('status', 1);
    }

    public function contents()
    {
        return $this->hasMany('App\Models\GoodsContent');
    }

    public function slideshow()
    {
        return $this->hasMany('App\Models\GoodsSlideshow');
    }

    public function shop()
    {
        return $this->belongsTo('App\Models\Shop');
    }

    public function freight()
    {
        return $this->belongsTo('App\Models\Freight');
    }
}
